package com.coastline.fruit.usercenter.service.impl;

import com.coastline.fruit.common.constants.CommonConstant;
import com.coastline.fruit.common.email.MailUtil;
import com.coastline.fruit.common.exception.CoastLineFruitException;
import com.coastline.fruit.common.utils.ValidateUtils;
import com.coastline.fruit.feign.clients.manage.OperatorClient;
import com.coastline.fruit.manage.request.CreateOperatorReq;
import com.coastline.fruit.usercenter.constants.UserCenterConstant;
import com.coastline.fruit.usercenter.entity.User;
import com.coastline.fruit.usercenter.entity.dao.UserDao;
import com.coastline.fruit.usercenter.exception.UserCenterErrorInfo;
import com.coastline.fruit.usercenter.mapper.UserMapper;
import com.coastline.fruit.usercenter.request.CreateUserReq;
import com.coastline.fruit.usercenter.response.CreateUserRsp;
import com.coastline.fruit.usercenter.service.UserService;
import com.coastline.fruit.usercenter.service.ansy.UserThreadPool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;

import javax.annotation.Resource;

/**
 * 用户管理实现类
 *
 * @author Coastline
 * @date 2025/1/1 22:27
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private OperatorClient operatorClient;

    @Resource
    private MailUtil mailUtil;

    @Resource
    private TemplateEngine templateEngine;

    @Override
    public CreateUserRsp createUser(CreateUserReq req) throws CoastLineFruitException {
        // 请求体参数校验
        checkCreateUserParams(req);

        // 校验用户是否已存在
        User user = req.getUser();
        checkIsUserExists(user);

        // 调用创建操作员接口 创建操作员信息
        try {
            userMapper.createUser(req.getUser());

            // 调用创建操作员接口 创建操作员信息
            CreateOperatorReq createOperatorReq = new CreateOperatorReq();
            createOperatorReq.setCreateOperatorId(req.getOperator().getCreateOperatorId());
            operatorClient.createOperator(createOperatorReq);
        } catch (Exception e) {
            log.error("数据库操作异常", e);
            throw new CoastLineFruitException(UserCenterErrorInfo.DATABASE_OPERATION_ERROR);
        }
        return new CreateUserRsp();
    }

    private void checkIsUserExists(User user) throws CoastLineFruitException {
        // 默认登录账户为空时 使用email作为登录账户
        if (StringUtils.isBlank(user.getLoginAccount())) {
            user.setLoginAccount(user.getEmail());
        }
        int result;
        try {
            result = userMapper.selectByUserName(user.getUserName(), user.getLoginAccount(), user.getEmail());
        } catch (Exception e) {
            log.error("数据库操作异常", e);
            throw new CoastLineFruitException(UserCenterErrorInfo.DATABASE_OPERATION_ERROR);
        }
        if (result > 0) {
            log.error("用户名称已存在");
            throw new CoastLineFruitException(UserCenterErrorInfo.USER_NAME_ALREADY_EXISTS);
        }
    }

    /**
     * 校验创建用户请求参数
     *
     * @param req 请求体
     * @throws CoastLineFruitException 异常
     */
    private void checkCreateUserParams(CreateUserReq req) throws CoastLineFruitException {
        ValidateUtils.checkMandatoryFiled(req.getReqId(), CommonConstant.REQ_ID, UserCenterErrorInfo.MANDATORY_PARAMS_IS_EMPTY);
        ValidateUtils.checkLength(req.getReqId(), CommonConstant.REQ_ID, CommonConstant.THIRTY_TWO, UserCenterErrorInfo.PARAM_LENGTH_TOO_LONG);
        ValidateUtils.checkMandatoryFiled(req.getUser(), UserCenterConstant.USER, UserCenterErrorInfo.MANDATORY_PARAMS_IS_EMPTY);
        ValidateUtils.checkMandatoryFiled(req.getUser().getUserName(), UserCenterConstant.USER_NAME, UserCenterErrorInfo.MANDATORY_PARAMS_IS_EMPTY);
        ValidateUtils.checkMandatoryFiled(req.getUser().getEmail(), UserCenterConstant.EMAIL, UserCenterErrorInfo.MANDATORY_PARAMS_IS_EMPTY);
        ValidateUtils.checkMandatoryFiled(req.getUser().getRole(), UserCenterConstant.ROLE, UserCenterErrorInfo.MANDATORY_PARAMS_IS_EMPTY);
        ValidateUtils.checkMandatoryFiled(req.getOperator(), CommonConstant.OPERATOR, UserCenterErrorInfo.MANDATORY_PARAMS_IS_EMPTY);
        ValidateUtils.checkLength(req.getUser().getUserName(), UserCenterConstant.USER_NAME, CommonConstant.THIRTY_TWO, UserCenterErrorInfo.PARAM_LENGTH_TOO_LONG);
    }

    /**
     * 异步线程实现方法
     */
    private void asynProcessUser() {
        UserDao userDao = new UserDao();

        UserThreadPool.getInstance().execute(userDao);
    }
}
